<?php

namespace app\index\controller;

use app\model\Incomelog;
use think\exception\ValidateException;
use app\model\Member;
use app\model\Config;
use app\model\QrcodeMod;
use app\model\Agent;
use app\model\AgentLevel;

class AgentController extends Base
{
    public function amount()
    {
        $Membermob = new Member;
        $memberinfo = $Membermob->getUserByWechat();
        $data['base'] = $memberinfo;

        return $this->json(['data' => $data]);
    }

    public function poster()
    {
        $Membermob = new Member;
        $QrcodeMod = new QrcodeMod;
        $memberinfo = $Membermob->getUserByWechat();
        $Qrcodedata = $QrcodeMod->getUserQrcode($memberinfo['id'], 'pages/index/index');
        $config = Config::getconfig();
        $agent = Config::getconfig('agent');

        $data['sitename'] = $config['sys_title'];
        if (empty($agent['poster'])) {
            $data['poster'] = toimg('public/agentposter.jpg');
        } else {
            $data['poster'] = strongHttp(toimg($agent['poster']));
        }
        $data['nickName'] = $memberinfo['nickname'];
        $data['qrcode'] = $Qrcodedata;

        return $this->json(['data' => $data]);
    }
    public function check()
    {
        $uid = UID();
        if (!empty($uid)) {
            $data = Agent::where(['uid' => $uid, 'status' => 1])->find();
            // colonelTitle

            if (!empty($data)) {
                $data = $data->toArray();
            }

            $data['agent_level_mane']  = AgentLevel::getLevel($data['agent_level']);
            $data['colonelCount'] = Member::where(['pid'=>$uid, 'status'=>1])->count();
            $agentteam = Config::getconfig('agentteam');
            $data['colonelTitle'] = '';
            if($data['colonelCount'] >= $agentteam['recommend_quantity']){
                $data['colonelTitle'] = "[团长]";
            }
            $data['is_submitaudit'] = \app\model\Uploadminiprogram::getaudit(input('get.v', '', 'serach_in'));
            $data['income'] = Incomelog::where(['uid' => $uid])->sum('income');
        } else {
            $data = [];
        }

        return $this->json(['msg' => 'success', 'data' => $data]);
    }

    public function checkreg()
    {
        $uid = UID();
        if (!empty($uid)) {
            $data = Agent::where(['uid' => $uid])->find();
            if (!empty($data)) {
                $data = $data->toArray();
            }
        }

        return $this->json(['data' => $data]);
    }
}
